<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class IndexController extends Controller
{
    //view
    public function view(Request $request)
    {
         $title = "京东首页";
         $conf = DB::table('bro_config') -> first();
        //会话跟踪
        $user = $request->session()->get('user');
         //获取所有大类别
        $type = DB::table('bro_cate') -> where('pid',1) -> get();
        // $id = $type[0] -> id;
        //获取各类别的小类别
        foreach($type as $v)
        {
            $id  = $v -> id;
            // $id = $type[$i] -> id;
            // dd($v);
            $typechild = DB::table('bro_cate') -> where('pid',$id) -> get();
            // dd($typechild);
            $typechilds[] = $typechild;
        }
//        dd($type);
        //获取服装的品牌
        $brands = DB::table('bro_brand') -> where('status','=',0) -> get();
        // dd($brands);
        //获取文章
        $article = DB::table('bro_article') -> get();

        //获取推荐商品
        $tjgoods = DB::table('bro_goods') -> where('tuijian',1) -> get();

        //获取商品信息
        $lgoods = DB::table('bro_goods')->orderBy('id', 'desc')->take(6)->get();
      
        //今日大牌 获取
        $brandname= DB::table('bro_brand') -> where('id',10) -> select('brandname') -> first();
//        dd($brandname);
        $brand = $brandname -> brandname;
        $brandgoods = DB::table('bro_goods') -> where('brand',$brand) ->take(12) -> get();
        // dd($brandgoods);
        //抄底优惠
        $low = DB::table('bro_goods') -> where('price','<',300) -> get();

        //友情链接
        $link = DB::table('bro_link') -> orderBy('order', 'asc') -> get();
        // dd($link);

        $data = DB::table('bro_config') -> first() -> wz_status;

        if($data == 0)
        {
            return view('errors.503');
        }else{
        //判断用户是否登录
        if(!empty($user))
        {
            //获取购物车内的商品数量
            $cart = DB::table('bro_cart') ->where('uid',$user -> id) -> get();
            

            //获取商品的ID
            foreach($cart as $val)
            {
                $goodsid[] = $val -> goods_id;
            }

            return view('welcome') ->with(['cart' => $cart,'title' => $title,'type' => $type,'typechilds' => $typechilds,'user' => $user,'article' => $article,'brands' => $brands,'tjgoods' => $tjgoods,'lgoods' => $lgoods,'brandgoods' => $brandgoods,'low' => $low,'link' => $link,'conf' => $conf]);
        }
        // $typechild = DB::table('bro_cate') -> where('pid',$id) -> get();
        // dd($typechilds);
        return view('welcome') ->with(['title' => $title,'type' => $type,'typechilds' => $typechilds,'user' => $user,'article' => $article,'brands' => $brands,'tjgoods' => $tjgoods,'lgoods' => $lgoods,'brandgoods' => $brandgoods,'low' => $low,'link' => $link,'conf' => $conf]);
    }
     // //网站开关
    	// $data = DB::table('bro_config') -> first() -> wz_status;
    	// if($data == 0)
    	// {
    	// 	return view('errors.503');
    	// }else{
    	// 	return view('welcome');
    	// }
    }

    // public function show(Request $request)
    // {
    //     $title = "京东首页";
    //     $conf = DB::table('bro_config') -> first();
    //     //会话跟踪
    //     $user = $request->session()->get('user');
    //     //获取所有大类别
    //     $type = DB::table('bro_cate') -> where('pid',1) -> get();
    //     // $id = $type[0] -> id;
    //     //获取各类别的小类别
    //     foreach($type as $v)
    //     {
    //         $id  = $v -> id;
    //         // $id = $type[$i] -> id;
    //         // dd($v);
    //         $typechild = DB::table('bro_cate') -> where('pid',$id) -> get();
    //         // dd($typechild);
    //         $typechilds[] = $typechild;
    //     }
        
    //     //获取文章
    //     $article = DB::table('bro_article') -> get();

    //     //判断用户是否登录
    //     if(!empty($user))
    //     {
    //         //获取购物车内的商品数量
    //         $cart = DB::table('bro_cart') ->where('uid',$user -> id) -> get();
            
    //         foreach($cart as $val)
    //         {
    //             $goodsid[] = $val -> goods_id;
    //         }

    //         return view('welcome') ->with(['cart' => $cart,'title' => $title,'type' => $type,'typechilds' => $typechilds,'user' => $user,'article' => $article,'conf' => $conf]);
    //     }
        

    //     return view('welcome') ->with(['title' => $title,'type' => $type,'typechilds' => $typechilds,'user' => $user,'article' => $article,'conf' => $conf]);
    // }
}
